1 00:00:00,630 --> 00:00:05,730 In this lecture, we're going to be using the badge service to award a badge to our player. 2 00:00:05,730 --> 00:00:07,110 When they win the game. 3 00:00:07,110 --> 00:00:11,550 This badge will be made permanently available within the account of the player who earns it. 4 00:00:11,550 --> 00:00:18,030 So the plan is to award every player that is alive on the map when the final wave is beaten, and give 5 00:00:18,030 --> 00:00:19,530 them a badge. 6 00:00:20,590 --> 00:00:26,470 In order to add a new badge to our game, we can head to the Creator dashboard on Roblox from our home 7 00:00:26,470 --> 00:00:28,600 page, and we can hit create. 8 00:00:30,000 --> 00:00:35,790 And now we are in the Creator Hub and we can go ahead and find our creations underneath the creations 9 00:00:35,790 --> 00:00:36,300 tab. 10 00:00:36,300 --> 00:00:39,840 Or I can find mine right here, which is my example wave game. 11 00:00:39,840 --> 00:00:46,200 So I'll select my game here, and then we can head over to the engagement section for our game. 12 00:00:46,200 --> 00:00:49,350 And down in this section right here you can find badges. 13 00:00:49,350 --> 00:00:51,510 And right now I don't have any in this experience. 14 00:00:51,510 --> 00:00:54,090 But we can go ahead and create a new badge. 15 00:00:54,090 --> 00:00:58,800 And here we can give it a name, a description and then an image for the badge. 16 00:00:59,810 --> 00:01:08,270 So I'm just going to call this badge you survived and give it some description like you successfully. 17 00:01:09,380 --> 00:01:11,930 Beat all the waves in the game. 18 00:01:11,930 --> 00:01:13,070 Something like that. 19 00:01:13,250 --> 00:01:17,240 And then you can also go ahead and upload an image for the badge as well, which I'm going to do. 20 00:01:17,880 --> 00:01:22,590 And once you've got your image uploaded for your badge, you can go ahead and hit the create badge function. 21 00:01:22,590 --> 00:01:25,500 And it also warns us that the first five badge creations are free. 22 00:01:25,500 --> 00:01:28,590 And then you have to pay 100 Robux for any additional badges. 23 00:01:28,590 --> 00:01:30,840 So we'll go ahead and create this badge. 24 00:01:30,840 --> 00:01:31,740 There we go. 25 00:01:31,740 --> 00:01:33,660 We have the you survived badge. 26 00:01:33,660 --> 00:01:37,140 And the important thing we're going to need from this badge is its ID. 27 00:01:37,140 --> 00:01:42,060 So we can hit these three dots and copy the asset ID of this badge. 28 00:01:42,840 --> 00:01:47,550 Now back inside of studio, we're going to need this badge in order to award it to our player. 29 00:01:47,580 --> 00:01:52,920 Now I want to go ahead and store any badges in my game inside of a module script that's going to be 30 00:01:52,920 --> 00:01:54,270 acting as an enum. 31 00:01:54,270 --> 00:02:00,780 That way we have a standard and know exactly what ID belongs to what, so we could correlate the id 32 00:02:00,810 --> 00:02:03,360 we just copied with the victory badge. 33 00:02:03,660 --> 00:02:06,360 So what we can go ahead and do is inside of replicated storage. 34 00:02:06,360 --> 00:02:10,380 Inside of the modules folder we'll go and create a new module script. 35 00:02:10,380 --> 00:02:14,550 And I'm going to call this my badge enum. 36 00:02:14,940 --> 00:02:20,850 And we're simply just going to return a table that's going to contain the different badge IDs. 37 00:02:20,850 --> 00:02:23,310 We would like to be able to reward to our players. 38 00:02:23,310 --> 00:02:26,640 I'm going to call this one my Victory Badge. 39 00:02:27,650 --> 00:02:29,570 And I'm going to set it equal to a table. 40 00:02:29,570 --> 00:02:32,420 And this table is going to contain the ID of the badge. 41 00:02:32,420 --> 00:02:35,420 So I'll just paste my ID right there for the badge. 42 00:02:35,420 --> 00:02:38,870 And then I can also enter in the name for my badge as well. 43 00:02:38,870 --> 00:02:43,070 So the name of my badge I'll just say victory badge. 44 00:02:43,070 --> 00:02:45,950 And of course we can use this name for the future. 45 00:02:45,950 --> 00:02:51,770 For example, if you wanted to give any players like special benefits in the game when they have a particular 46 00:02:51,770 --> 00:02:56,570 badge, you can check if they have this badge, and then you could apply an attribute on them with this 47 00:02:56,570 --> 00:02:57,110 name. 48 00:02:57,110 --> 00:03:01,610 And then from that point, any script could check if a player has this attribute on them and then give 49 00:03:01,610 --> 00:03:03,500 them some kind of special benefit. 50 00:03:04,210 --> 00:03:10,540 And then we can go back to our game handler server script and go ahead and grab the badge service in 51 00:03:10,540 --> 00:03:13,090 order to award a badge to our player. 52 00:03:13,330 --> 00:03:16,270 And then we can also get our badge enum. 53 00:03:16,840 --> 00:03:19,270 Will require for replicated storage modules. 54 00:03:19,450 --> 00:03:19,840 Enums. 55 00:03:19,840 --> 00:03:21,070 Dot badge enum. 56 00:03:22,380 --> 00:03:27,030 And then I'm going to create a dedicated function for awarding a badge to a player. 57 00:03:27,300 --> 00:03:33,870 I'll call this function award badge and we'll pass the ID of the player. 58 00:03:33,870 --> 00:03:35,310 So we'll call this player ID. 59 00:03:35,310 --> 00:03:40,080 And then we also need the badge ID we want to award to this particular player. 60 00:03:40,560 --> 00:03:44,700 Then inside of the badge service we have a function called award badge. 61 00:03:44,700 --> 00:03:48,150 And it says Award a badge to a player given the ID of each. 62 00:03:48,150 --> 00:03:51,510 So we pass the user ID of the player and then the badge ID. 63 00:03:51,810 --> 00:03:56,910 So we'll pass our player ID and then we'll pass our badge ID. 64 00:03:57,880 --> 00:04:05,140 And then I'm going to wrap this function in a p call, just in case this award badge function encounters 65 00:04:05,140 --> 00:04:05,770 a problem. 66 00:04:05,770 --> 00:04:10,210 So I'm going to call this success results is equal to p call. 67 00:04:12,900 --> 00:04:17,850 We're going to paste that in there and return the result from our award badge function. 68 00:04:18,330 --> 00:04:22,800 And then we can check to see if we were successful in awarding a badge to this player. 69 00:04:22,800 --> 00:04:29,850 If we were not successful, then we could just print something in the console like failed to award badge, 70 00:04:29,940 --> 00:04:31,890 and then we could give the reason why. 71 00:04:31,890 --> 00:04:39,360 And I'll print that on a new line with backslash n, and then we can concatenate it with the results 72 00:04:39,360 --> 00:04:40,530 from our p call. 73 00:04:42,280 --> 00:04:48,400 And now what we can go ahead and do is use this function to award a badge to all of the players that 74 00:04:48,400 --> 00:04:51,820 have beaten the final wave, and they're still alive. 75 00:04:52,440 --> 00:04:54,540 So we can go ahead and scroll down. 76 00:04:55,090 --> 00:05:01,540 And when the function for the game over executes for when the players win, well, we can go ahead and 77 00:05:01,540 --> 00:05:07,810 do is after we play the victory sound, we'll loop through every single player that's on the alive team. 78 00:05:07,810 --> 00:05:14,170 So in Teams Alive, get players and we'll award a badge to all these players. 79 00:05:14,170 --> 00:05:19,870 So we'll pass the player dot user ID and then we'll also pass the badge ID we would like to award to 80 00:05:19,870 --> 00:05:22,120 the player, which would be inside of our badge enum. 81 00:05:22,120 --> 00:05:24,070 And that's going to be the victory badge. 82 00:05:24,070 --> 00:05:32,170 And we're going to get that ID now in order to test this, what I'm going to do is I'm going to decrease. 83 00:05:32,690 --> 00:05:34,820 My total waves amount to one. 84 00:05:34,820 --> 00:05:38,000 That way I don't have to sit through 11 waves. 85 00:05:38,000 --> 00:05:43,460 So once we beat the first wave, then this loop's going to think that all of the players have won, 86 00:05:43,460 --> 00:05:48,950 and then it should award us the badge that, you know, we've won the game. 87 00:05:48,950 --> 00:05:52,610 So let's go ahead and play test this game. 88 00:05:54,610 --> 00:05:55,450 The waves starting. 89 00:05:55,450 --> 00:05:58,360 So let's go ahead and kill all of these zombies off. 90 00:06:00,910 --> 00:06:02,830 All right, here's my last three zombies. 91 00:06:02,830 --> 00:06:07,660 And if I kill all of these guys, hopefully I get awarded that badge. 92 00:06:07,660 --> 00:06:09,670 So when I kill this guy, we should win. 93 00:06:09,670 --> 00:06:10,810 Let's see what happens. 94 00:06:12,160 --> 00:06:12,580 Oh! 95 00:06:13,240 --> 00:06:13,930 Game over. 96 00:06:13,930 --> 00:06:14,470 We won. 97 00:06:14,470 --> 00:06:15,220 And there we go. 98 00:06:15,220 --> 00:06:16,150 Badge awarded. 99 00:06:16,150 --> 00:06:19,030 We got the you Survived award. 100 00:06:19,030 --> 00:06:22,120 And now that badge has been added to our account. 101 00:06:22,120 --> 00:06:23,170 Very cool. 102 00:06:24,710 --> 00:06:28,760 That's all I have for you in this lecture, and I'll see you in the next one.